//
//  ContattiViewController.m
//  bApp
//
//  Created by Guglielmo Buzzelli on 15/05/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ContattiViewController.h"

@interface ContattiViewController ()

@end

@implementation ContattiViewController
@synthesize contattiView;
@synthesize mapView;
@synthesize navbtn;
@synthesize scrollView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSTimer *mytimer = [NSTimer timerWithTimeInterval:10.0 target:self selector:@selector(start) userInfo:nil repeats:YES];
    
    [[NSRunLoop mainRunLoop] addTimer:mytimer forMode:NSDefaultRunLoopMode];
    
    [self start];
    
    UITextView *text1part1 = [[UITextView alloc]initWithFrame:CGRectMake(20, 20, 280, 200)];
    
    NSDictionary *info = [json objectAtIndex:0];
    NSString *comeRaggiungere = [NSString stringWithFormat:@"%@",[info objectForKey:@"come_raggiungere"]];
    
    /*text1part1.text=@"Il ristorante Sasseo si trova sulle dolci colline dell'Oltrepo pavese, a 12 km da Stradella e facilmente raggiungibile dall'autostrada A21 - uscita broni/Stradella.A Stradella seguire le indicazioni per S.Maria della Versa; attraversato il centro di S.Maria, proseguire lungo la strada per Zavattarello/Montecalvo Versilia.";*/
    
    text1part1.text = comeRaggiungere;
    text1part1.backgroundColor = [UIColor clearColor];
    text1part1.editable = NO;
    [self.scrollView addSubview:text1part1];
    
    UIImageView *imgText2part1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10 , 300, 200)];
    imgText2part1.image = [UIImage imageNamed:@"box_text@2x.png"];
    [self.scrollView addSubview:imgText2part1];
    [self.scrollView sendSubviewToBack:imgText2part1];
    
    self.scrollView.contentSize = CGSizeMake( self.scrollView.frame.size.width , text1part1.frame.size.height);
    
    
    
    UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back_ground_app@2x.png"]];
    
    [self.view addSubview:backgroundImage ];
    [self.view sendSubviewToBack:backgroundImage ];
    
/*inserimento diapositiva 1 di sfondo con crop e rotation*/
    UIImage *myImage = [UIImage imageNamed:@"piatto1.jpg"];
    CGRect cropRect = CGRectMake(89.0, 40.0, 160.0, 160.0);
    CGImageRef croppedImage = CGImageCreateWithImageInRect([myImage CGImage], cropRect);
    UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
    [myImageView setImage:[UIImage imageWithCGImage:croppedImage]]; 
    CGImageRelease(croppedImage);
    myImageView.transform =CGAffineTransformMakeRotation(12.0*M_PI/180.0);
    [backgroundImage addSubview: myImageView];
    [backgroundImage sendSubviewToBack:myImageView];
    
/*inserimento diapositiva 1 di sfondo con crop e rotation*/  
    UIImage *myImage2 = [UIImage imageNamed:@"piatto2.jpg"];
    CGRect cropRect2 = CGRectMake(95.0, 40.0, 150.0, 150.0);
    CGImageRef croppedImage2 = CGImageCreateWithImageInRect([myImage2 CGImage], cropRect2);
    UIImageView *myImageView2 = [[UIImageView alloc] initWithFrame:cropRect2];
    [myImageView2 setImage:[UIImage imageWithCGImage:croppedImage2]]; 
    CGImageRelease(croppedImage2);
    myImageView2.transform =CGAffineTransformMakeRotation(24.0*M_PI/180.0);
    [backgroundImage addSubview: myImageView2];
    [backgroundImage sendSubviewToBack:myImageView2];
    
    //navbtn.transform =CGAffineTransformMakeRotation(-8.0*M_PI/180.0);
    

    
}

-(void)getData:(NSData *) data{
    
    NSError *error;
    
    if(data!=nil)
        json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error ];

    
}

-(void) start{
    
    NSURL *url = [NSURL URLWithString:kGETUrlMap];
    
    NSData *data = [NSData dataWithContentsOfURL:url];
    
    [self getData:data];
    
}

- (void)viewDidUnload
{
    [self setContattiView:nil];

    [self setMapView:nil];
    [self setNavbtn:nil];
    [self setScrollView:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)viewWillAppear:(BOOL)animated{
    
    NSDictionary *info = [json objectAtIndex:0];
    NSString *latitudine = [NSString stringWithFormat:@"%@",[info objectForKey:@"latitudine"]];
    NSString *longitudine = [NSString stringWithFormat:@"%@",[info objectForKey:@"longitudine"]];
    
    
    CGFloat x = [latitudine floatValue];
    CGFloat y = [longitudine floatValue];
    
    CLLocationCoordinate2D zoomLocation;
    //zoomLocation.latitude = 42.356911;
    //zoomLocation.longitude = 14.405211;
    zoomLocation.latitude = x;
    zoomLocation.longitude = y;


    
    NSLog(@"%f,%f",x,y);
    
    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
    
    MKCoordinateRegion adjustRegion = [mapView regionThatFits:viewRegion];
    
    MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
    annotationPoint.coordinate = zoomLocation;
    annotationPoint.title = [info objectForKey:@"titolo_marker"];
    annotationPoint.subtitle = [info objectForKey:@"sottotitolo_marker"];

    [mapView addAnnotation:annotationPoint]; 
    
    [mapView setRegion:adjustRegion animated:YES];
    
    mapView.showsUserLocation = YES;
    
    //MKMapPointForCoordinate(zoomLocation);
    
    mapView.transform =CGAffineTransformMakeRotation(-8.0*M_PI/180.0);


    
    
}

- (IBAction)avviaNavigazione:(id)sender {
    
    NSDictionary *info = [json objectAtIndex:0];
    /*NSString *latitudine = [NSString stringWithFormat:@"%@",[info objectForKey:@"latitudine"]];
    NSString *longitudine = [NSString stringWithFormat:@"%@",[info objectForKey:@"longitudine"]];
    

    NSString *latlong = [NSString stringWithFormat:@"%@,%@",latitudine,longitudine];
    NSString *url = [NSString stringWithFormat: @"http://maps.google.com/maps?ll=%@",
                     [latlong stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];*/
    
    NSString *url = [info objectForKey:@"url_gmap_breve"];
    
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];

    
}
@end
